/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_order_goods.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuecai.modules.activity_coupon.mapper.YcOrderGoodMapper;
import com.yuecai.modules.yc_order_goods.domain.YcOrderGoods;
import com.yuecai.modules.yc_order_goods.repository.YcOrderGoodsRepository;
import com.yuecai.modules.yc_order_goods.service.dto.OrderGoodsDto;
import com.yuecai.modules.yc_order_goods.service.dto.YcOrderGoodsDto;
import com.yuecai.modules.yc_order_goods.service.dto.YcOrderGoodsQueryCriteria;
import com.yuecai.modules.yc_order_goods.service.mapstruct.YcOrderGoodsMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author DM
* @date 2020-06-26
**/
@Service
@RequiredArgsConstructor
public class YcOrderGoodsService {

    private final YcOrderGoodsRepository ycOrderGoodsRepository;
    private final YcOrderGoodsMapper ycOrderGoodsMapper;

    
    public Map<String,Object> queryAll(YcOrderGoodsQueryCriteria criteria, Pageable pageable){
        Page<YcOrderGoods> page = ycOrderGoodsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycOrderGoodsMapper::toDto));
    }

    
    public List<YcOrderGoodsDto> queryAll(YcOrderGoodsQueryCriteria criteria){
        return ycOrderGoodsMapper.toDto(ycOrderGoodsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    
    @Transactional
    public YcOrderGoodsDto findById(Long id) {
        YcOrderGoods ycOrderGoods = ycOrderGoodsRepository.findById(id).orElseGet(YcOrderGoods::new);
        ValidationUtil.isNull(ycOrderGoods.getId(),"YcOrderGoods","id",id);
        return ycOrderGoodsMapper.toDto(ycOrderGoods);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public YcOrderGoodsDto create(YcOrderGoods resources) {
        return ycOrderGoodsMapper.toDto(ycOrderGoodsRepository.save(resources));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(YcOrderGoods resources) {
        YcOrderGoods ycOrderGoods = ycOrderGoodsRepository.findById(resources.getId()).orElseGet(YcOrderGoods::new);
        ValidationUtil.isNull( ycOrderGoods.getId(),"YcOrderGoods","id",resources.getId());
        ycOrderGoods.copy(resources);
        ycOrderGoodsRepository.save(ycOrderGoods);
    }

    
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            ycOrderGoodsRepository.deleteById(id);
        }
    }

    private final YcOrderGoodMapper ycOrderGoodMapper;


    public ResponseEntity<Object> queryOrderGoods(OrderGoodsDto dto) {
        IPage page = ycOrderGoodMapper.queryOrderGoods(dto);
        return ResponseEntity.ok(page);
    }


    
    public void download(List<YcOrderGoodsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcOrderGoodsDto ycOrderGoods : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单编号", ycOrderGoods.getOrderSn());
            map.put("商品ID", ycOrderGoods.getGoodsId());
            map.put("名称", ycOrderGoods.getGoodsName());
            map.put("主图", ycOrderGoods.getImg());
            map.put("数量", ycOrderGoods.getCount());
            map.put("单位名", ycOrderGoods.getUnitName());
            map.put("原价", ycOrderGoods.getOriginalPrice());
            map.put("销售价", ycOrderGoods.getSellPrice());
            map.put("vip折扣 例：八折填80", ycOrderGoods.getVipDiscount());
            map.put("活动id", ycOrderGoods.getActivityId());
            map.put("活动详情", ycOrderGoods.getActivityDetails());
            map.put("活动名称", ycOrderGoods.getActivityName());
            map.put("创建时间", ycOrderGoods.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}